<script lang="ts">
	import { createEventDispatcher, onMount } from 'svelte'
	import { Badge, Skeleton } from '$lib/components/common'
	import SearchItems from '$lib/components/SearchItems.svelte'
	import ListFilters from '$lib/components/home/ListFilters.svelte'
	import NoItemFound from '$lib/components/home/NoItemFound.svelte'
	import RowIcon from '$lib/components/common/table/RowIcon.svelte'
	import { loadHubFlows } from '$lib/hub'

	export let filter = ''
	export let syncQuery = false

	type Item = { apps: string[]; summary: string; path: string }
	let hubFlows: any[] | undefined = undefined
	let filteredItems: (Item & { marked?: string })[] = []
	let appFilter: string | undefined = undefined

	$: prefilteredItems = appFilter
		? (hubFlows ?? []).filter((i) => i.apps.includes(appFilter))
		: hubFlows ?? []

	$: apps = Array.from(new Set(filteredItems?.flatMap((x) => x.apps) ?? [])).sort()

	const dispatch = createEventDispatcher()

	onMount(async () => {
		hubFlows = await loadHubFlows()
	})
</script>

<SearchItems
	{filter}
	items={prefilteredItems}
	bind:filteredItems
	f={(x) => x.summary + ' (' + x.apps.join(', ') + ')'}
/>
<div class="w-full flex mt-1 items-center gap-2">
	<slot />
	<input type="text" placeholder="Search Hub Flows" bind:value={filter} class="text-2xl grow" />
</div>
<ListFilters {syncQuery} filters={apps} bind:selectedFilter={appFilter} resourceType />

{#if hubFlows}
	{#if filteredItems.length == 0}
		<NoItemFound />
	{:else}
		<ul class="divide-y border rounded-md overflow-hidden">
			{#each filteredItems as item (item)}
				<li class="flex flex-row w-full">
					<button
						class="p-4 gap-4 flex flex-row grow justify-between hover:bg-surface-hover bg-surfacehite transition-all items-center"
						on:click={() => dispatch('pick', item)}
					>
						<div class="flex items-center gap-4">
							<RowIcon kind="flow" />

							<div class="w-full text-left font-normal">
								<div class="text-primary flex-wrap text-md font-semibold">
									{#if item.marked}
										{@html item.marked ?? ''}
									{:else}
										{item.summary ?? ''}
									{/if}
								</div>
							</div>
						</div>
						<div class="min-w-1/3 gap-2 flex flex-wrap justify-end">
							{#each item.apps as app}
								<Badge color="gray" baseClass="border">{app}</Badge>
							{/each}
						</div>
					</button>
				</li>
			{/each}
		</ul>
	{/if}
{:else}
	<div class="my-2" />

	{#each Array(10).fill(0) as _}
		<Skeleton layout={[[4], 0.5]} />
	{/each}
{/if}
